package com.usian.controller;

import com.usian.User;
import com.usian.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    UserFeign userFeign;



    @RequestMapping(value="/user/{id}",method= RequestMethod.GET)
    public User getUserBy(@PathVariable Integer id){
        ServiceInstance si = loadBalancerClient.choose("ribbon-provider");
        //指定要调用的服务
        String url = "http://"+si.getHost()+":"+si.getPort()+"/user/findUserByUserId/"+id;
        return restTemplate.getForObject(url, User.class);
    }


 @RequestMapping(value="/feign/{id}",method= RequestMethod.GET)
    public User getUserByFeign(@PathVariable Integer id){
     User userId = userFeign.findUserByUserId(id);
         return  userId;

        }


@RequestMapping(value="/feign/findByid",method= RequestMethod.GET)
    public User findByid(@RequestParam Integer id){
     User userId = userFeign.findById(id);
         return  userId;

        }


@RequestMapping(value="/feign/insertUser")
    public String insertUser(@RequestBody User user){
     String result = userFeign.insertUser(user);
         return  result;

        }





}